package mt.dummy;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

/**
 * Creates the table used in MicroTraderClientDummy, using the java class
 * AbstractTableModel
 */

public class ClientTable extends AbstractTableModel {
        private static final long serialVersionUID = 1L;

        ArrayList<Object[]> oL;
        String[] header;

        ClientTable(Object[] obj, String[] header) {
                this.header = header;
                oL = new ArrayList<Object[]>();
        }

        public int getRowCount() {
                return oL.size();
        }

        public int getColumnCount() {
                return header.length;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
                return oL.get(rowIndex)[columnIndex];
        }

        public String getColumnName(int index) {
                return header[index];
        }

        void add(String stock, String numberOfUnits, String pricePerUnit,
                        boolean buyOrder, int id) {
                boolean foundStock = false;
                for (int i = 0; i < oL.size(); i++) {
                        if (oL.get(i)[0].equals(stock)) {
                                foundStock = true;
                                int nUnits = Integer.parseInt(numberOfUnits);
                                System.out.println("NUnits: " + numberOfUnits );
                                if (!numberOfUnits.equals("0")) {
                                        if (buyOrder) {
                                                oL.get(i)[1] = "" + nUnits;
                                                oL.get(i)[3] = "BUY";
                                        }  if (!buyOrder) {
                                                oL.get(i)[1] = "" + nUnits;
                                                oL.get(i)[3] = "SELL";
                                        }
                                } else {
                                        oL.remove(oL.get(i));
                                }
                        }
                }
                if (!foundStock) {
                        String[] str = new String[4];
                        str[0] = stock;
                        str[1] = numberOfUnits;
                        str[2] = pricePerUnit;
                        if (buyOrder) {
                                str[3] = "BUY";
                        } else {
                                str[3] = "SELL";
                        }
                        oL.add(str);
                        System.out.println(oL.size() + "   <=======   SIZE" );
                }       
                // Informs the GUI that theTable has changed its list information.
                fireTableDataChanged();
        }
}